﻿using Engine;
using Game;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewST
{
    public class StyleOnMinerHitGenerated : ModLoader
    {
        public SubsystemParticles m_subsystemParticles;
        public override void __ModInitialize()
        {
            ModsManager.RegisterHook("OnMinerHit", this);
        }
        public override void OnMinerHit(ComponentMiner miner, ComponentBody componentBody, Vector3 hitPoint, Vector3 hitDirection, ref float attackPower, ref float playerProbability, ref float creatureProbability, out bool Hitted)
        {
            Hitted = false;
            Ray3 ray = new Ray3(hitPoint, hitDirection);
            object rayCastResult = miner.Raycast(ray,RaycastMode.Interaction);
            IInventory inventory = miner.Inventory;
            int activeBlockValue = miner.ActiveBlockValue;
            int blockIndex = Terrain.ExtractContents(activeBlockValue);
            if (rayCastResult is TerrainRaycastResult)
            {
                CellFace cellFace = ((TerrainRaycastResult)rayCastResult).CellFace;
                Point3 hitCell = cellFace.Point;
                int id = miner.m_subsystemTerrain.Terrain.GetCellContents(hitCell.X, hitCell.Y, hitCell.Z);

                if (blockIndex == BlocksManager.GetBlockIndex<NewST.XZStone>())
                {
                    if (id == BlocksManager.GetBlockIndex<NewST.XZStone>())
                    {
                        Point3 neighborCell = new Point3(
                        hitCell.X + 1,
                        hitCell.Y,
                        hitCell.Z
                        );
                        XZZSEffects(miner,hitPoint,id,neighborCell);
                    }
                }
            }
        }

        private void XZZSEffects(ComponentMiner miner, Vector3 hitPoint,int id, Point3 neighborCell)
        {
            m_subsystemParticles.AddParticleSystem(new FireworksParticleSystem(hitPoint, Color.Blue, FireworksBlock.Shape.SmallBurst, 0.3f, 0.5f));
            m_subsystemParticles.AddParticleSystem(new HitValueParticleSystem(hitPoint + new Vector3(0, 0.5f, 0), new Vector3(0, 0.5f, 0), Color.LightBlue, "复制"));
            miner.m_subsystemTerrain.ChangeCell(neighborCell.X, neighborCell.Y, neighborCell.Z, id);
        }
    }
}
